package megajournal.view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import megajournal.domain.Group;
import megajournal.domain.Student;
import megajournal.view.ocDialog;

/**
 *
 * @author cska
 * 
 */

public class StudentDlg extends JDialog implements ocDialog {

    protected GridBagConstraints c;
    protected JTextField fio;
    protected JLabel l;
    protected JPanel buttonPanel;
    protected JButton ok;
    protected JButton cancel;
    protected Student student;
    public boolean fl;

    public StudentDlg(JFrame owner, boolean modal, String title, Student student, Group group) {
        super(owner, modal);
        setTitle(title);
        fl = false;
        if (student != null) {
            this.student = student;
        } else {
            this.student = new Student("", group);
        }

        setLayout(new GridBagLayout());
        c = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        c.weightx = 0.1;
        l = new JLabel("Ф.И.О.:", JLabel.RIGHT);
        add(l, c);

        c.gridx = 1;
        c.weightx = 0.9;
        fio = new JTextField(this.student.getFio(), 5);
        add(fio, c);

        buttonPanel = new JPanel(new GridBagLayout());
        c.gridx = 0;
        c.weightx = 1.0;
        c.anchor = GridBagConstraints.EAST;
        c.fill = GridBagConstraints.CENTER;
        c.insets = new Insets(0, 0, 0, 5);
        ok = new JButton("Сохранить");
        ok.addActionListener(new okAdapter(this));
        buttonPanel.add(ok, c);
        c.weightx = 0.0;
        c.gridx = 1;
        c.insets = new Insets(0, 5, 0, 0);
        cancel = new JButton("Отмена");
        cancel.addActionListener(new cancelAdapter(this));
        buttonPanel.add(cancel, c);
        c.gridx = 0;
        c.gridy = 1;
        c.weightx = 1.0;
        c.anchor = GridBagConstraints.CENTER;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth = 2;
        c.insets = new Insets(5, 5, 5, 5);
        add(buttonPanel, c);

        setSize(320, 100);
        Dimension screen = getToolkit().getScreenSize().getSize();
        setLocation((screen.width - 320) / 2, (screen.height - 200) / 2);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public Student getStudent() {
        return student;
    }

    public void ok() {
        student.setFio(fio.getText());
        fl = true;
        setVisible(false);
    }

    public void cancel() {
        fl = false;
        setVisible(false);
    }

}
